package pam.web;

public abstract class AbstractPamletFactoryGroup implements IPamletFactory {

	public final AbstractPamlet getPamlet(String pamletName)
			throws WebException {

		if (pamletName == null) {
			throw new WebException("服务请求错误:未知请求!");
		}

		String[] factoryNames = getFactoryNames();
		if (factoryNames == null) {
			throw new WebException("没有指定FunctionFactory类名!");
		}

		for (int i = 0; i < factoryNames.length; i++) {
			String factoryClass = factoryNames[i];

			IPamletFactory factory;
			try {
				factory = (IPamletFactory) Class.forName(factoryClass)
						.newInstance();
			} catch (InstantiationException e) {
				e.printStackTrace();
				continue;
			} catch (IllegalAccessException e) {
				e.printStackTrace();
				continue;
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
				continue;
			}

			if (factory == null) {
				continue;
			}

			AbstractPamlet function = null;
			try {
				function = factory.getPamlet(pamletName);
			} catch (WebException e) {
			}

			if (function != null)
				return function;
		}

		throw new WebException("参数错误:未知请求!");
	}

	protected abstract String[] getFactoryNames();

}
